<?php

/**
 * This is the model class for table "{{receive}}".
 *
 * The followings are the available columns in table '{{receive}}':
 * @property string $idreceive
 * @property string $invoiceno
 * @property string $receivedate
 * @property string $pono
 * @property string $receivedfrom
 * @property string $clientaddress
 * @property string $vehicleno
 * @property string $drivername
 * @property string $drivercontact
 * @property string $receiveremark
 * @property string $vehiclecharge
 * @property integer $authorize
 *
 * The followings are the available model relations:
 * @property Dispatch[] $dispatches
 * @property Stockgroup $pono0
 * @property Acledger $receivedfrom0
 * @property Receivedetails[] $receivedetails
 */
class Receive extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Receive the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{receive}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('invoiceno, receivedate, pono, receivedfrom, clientaddress, vehicleno, drivername, drivercontact, receiveremark, vehiclecharge', 'required'),
			array('authorize', 'numerical', 'integerOnly'=>true),
			array('invoiceno, pono, receivedfrom, vehicleno, vehiclecharge', 'length', 'max'=>10),
			array('clientaddress, drivername', 'length', 'max'=>50),
			array('drivercontact', 'length', 'max'=>20),
			array('receiveremark', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idreceive, invoiceno, receivedate, pono, receivedfrom, clientaddress, vehicleno, drivername, drivercontact, receiveremark, vehiclecharge, authorize', 'safe', 'on'=>'search'),
                        array('stockgroupname', 'autocompleteValidator', 'model'=>'Stockgroup'),
                        array('acledgername', 'autocompleteValidator', 'model'=>'Acledger'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!$params['model']::model()->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['stockgroupname'])){
                $match = addcslashes($_REQUEST['stockgroupname'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'stockgroupname = :stockgroupname',
                    'params'    => array(':stockgroupname' => "$match")
                ) );            
                $model=Stockgroup::model()->find($criteria);
                $this->idstockgroup = $model->idstockgroup;
            }
                        if(isset($_REQUEST['acledgername'])){
                $match = addcslashes($_REQUEST['acledgername'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'acledgername = :acledgername',
                    'params'    => array(':acledgername' => "$match")
                ) );            
                $model=Acledger::model()->find($criteria);
                $this->idacledger = $model->idacledger;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'dispatches' => array(self::HAS_MANY, 'Dispatch', 'idreceive'),
			'pono0' => array(self::BELONGS_TO, 'Stockgroup', 'pono'),
			'receivedfrom0' => array(self::BELONGS_TO, 'Acledger', 'receivedfrom'),
			'receivedetails' => array(self::HAS_MANY, 'Receivedetails', 'idreceive'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idreceive' => Yii::t('application', 'Idreceive'),
			'invoiceno' => Yii::t('application', 'Invoiceno'),
			'receivedate' => Yii::t('application', 'Receivedate'),
			'pono' => Yii::t('application', 'Pono'),
			'receivedfrom' => Yii::t('application', 'Receivedfrom'),
			'clientaddress' => Yii::t('application', 'Clientaddress'),
			'vehicleno' => Yii::t('application', 'Vehicleno'),
			'drivername' => Yii::t('application', 'Drivername'),
			'drivercontact' => Yii::t('application', 'Drivercontact'),
			'receiveremark' => Yii::t('application', 'Receiveremark'),
			'vehiclecharge' => Yii::t('application', 'Vehiclecharge'),
			'authorize' => Yii::t('application', 'Authorize'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idreceive',$this->idreceive,true);
		$criteria->compare('invoiceno',$this->invoiceno,true);
		$criteria->compare('receivedate',$this->receivedate,true);
		$criteria->compare('pono',$this->pono,true);
		$criteria->compare('receivedfrom',$this->receivedfrom,true);
		$criteria->compare('clientaddress',$this->clientaddress,true);
		$criteria->compare('vehicleno',$this->vehicleno,true);
		$criteria->compare('drivername',$this->drivername,true);
		$criteria->compare('drivercontact',$this->drivercontact,true);
		$criteria->compare('receiveremark',$this->receiveremark,true);
		$criteria->compare('vehiclecharge',$this->vehiclecharge,true);
		$criteria->compare('authorize',$this->authorize);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}